package cn.k23.controller;

import cn.k23.mapper.CarouselMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @Description:
 * @Author: k23
 * @Date: 2022-04-04 16:14
 * @Version: 1.0
 */
@RestController
public class CarouselController {

    @Autowired
    private CarouselMapper cm;
    @PutMapping("updateisshow")
    public Map<String,Object> updateIsShow(@RequestBody Map<String,Object> mm){
        int v = Integer.parseInt(mm.get("v").toString());
        int id = Integer.parseInt(mm.get("id").toString());
        cm.updateIsShow(v,id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",200);
        map.put("msg",mm.get("t").toString()+":"+(v==0?"设置了隐藏了":"设置显示了"));
        return map;
    }

    @DeleteMapping("/delete")
    public Map<String,Object> delete(@RequestBody Map<String,Object> mm, HttpServletRequest req){
        //删除轮播图数据库信息
        int id = Integer.parseInt(mm.get("id").toString());
        cm.deleteById(id);

        //删除文件，轮播图片文件
        String f = req.getServletContext().getRealPath(mm.get("src").toString());
        File file = new File(f);
        if(file.exists()){
            file.delete();
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",200);
        map.put("msg",mm.get("t").toString()+":删除成功！");
        return map;
    }


    //添加轮播信息
    @GetMapping("/cal/add")
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView("cal/add");
        return mv;
    }


    //编辑轮播信息
    @GetMapping("/cal/edit")
    public ModelAndView edit(){
        ModelAndView mv = new ModelAndView("cal/edit");
        return mv;
    }

    //保存新建轮播信息
    @PostMapping  ("/cal/save")
    public Map<String,Object> save(MultipartFile file,String title,int isshow,String link,HttpServletRequest req) throws IOException {
       //处理上传文件
        String filename= file.getOriginalFilename().toLowerCase();

        LocalDateTime dt = LocalDateTime.now();
        var fmt1 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.CHINA);
        System.out.println(fmt1.format(dt));
        String ext = filename.substring(filename.lastIndexOf("."));
        filename = fmt1.format(dt)+ext;
        if(!file.isEmpty()){
            String path = req.getServletContext().getRealPath("/static/uploads");
            File ff = new File(path);
            if(!ff.exists())ff.mkdirs();
            file.transferTo(new File(path,filename));
        }
       //保存数据到数据库
        Map<String,Object> v = new HashMap<>();
        v.put("link",link);
        v.put("title",title);
        v.put("isshow",isshow);
        v.put("src","uploads/"+filename);
        cm.save(v);
       //返回信息
       Map<String,Object>map = new HashMap<>();
       map.put("code",200);
       map.put("msg","轮播添加成功");
       return map;
    }

    //批量删除轮播信息
    @DeleteMapping("/cal/delall")
    public Map<String,Object> delall(@RequestBody List<Map<String,Object>> list, HttpServletRequest req){
     for (Map<String,Object> m:list){
        //删除轮播图数据库记录
        int id = Integer.parseInt(m.get("id").toString());
        cm.deleteById(id);

        //删除本地文件，
         String f = req.getServletContext().getRealPath("/static/"+m.get("src").toString());
        File file = new File(f);
        if(file.exists()){
            file.delete();
        }
     }
     Map<String,Object> map = new HashMap<>();
     map.put("code",200);
     map.put("msg","删除成功");
     return map;
    }


}
